package main

import (
	"context"
	"encoding/json"
	"flag"
	"fmt"
	"github.com/hibiken/asynq"
	"t-go-zero/service/asynq/client/internal/config"
	"t-go-zero/service/asynq/client/internal/svc"
	"t-go-zero/service/asynq/jobtype"
	"time"

	"github.com/zeromicro/go-zero/core/conf"
)

var configFile = flag.String("f", "etc/asynq-client.yaml", "the config file")

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)

	// 中间件注册
	svcContext := svc.NewServiceContext(c)

	for i := 0; i < 100; i++ {

		fmt.Println("我们去执行任务了")

		// 创建上下文
		ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)

		user := jobtype.User{
			Id:   int64(i),
			Name: "超管",
		}

		data, err := json.Marshal(user)
		if err != nil {
			fmt.Println("序列化失败")
			continue
		}

		_, err = svcContext.AsynqClient.EnqueueContext(ctx, asynq.NewTask(jobtype.Add, data))

		if err != nil {
			fmt.Println(fmt.Errorf("错误的内容:%s", err.Error()))
		}

		fmt.Println("我们任务执行结束了")

		time.Sleep(time.Second * 5)

	}

}
